﻿using System.Net;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace BaseSet.Entity.Common.Web
{
    public class GetWebPage
    {
        /// <summary>
        /// 以HttpWeb方式抓取网页内容
        /// </summary>
        /// <param name="sUrl">网页地址</param>
        /// <returns>string:网页内容</returns>
        public static string GetWebPageByHttpWeb(string sUrl)
        {
            return GetWebPageByHttpWeb(sUrl, GetWebPageEncoding(sUrl));
        }
        /// <summary>
        /// 以HttpWeb方式抓取网页内容
        /// </summary>
        /// <param name="sUrl">网页地址</param>
        /// <param name="encoding">编码方式</param>
        /// <returns>string:网页内容</returns>
        public static string GetWebPageByHttpWeb(string sUrl, Encoding encoding)
        {
            return GetWebPageByHttpWeb(sUrl, encoding, "zh-CN,en-US;q=0.5");
        }
        /// <summary>
        /// 以HttpWeb方式抓取网页内容
        /// </summary>
        /// <param name="sUrl">网页地址</param>
        /// <param name="encoding">编码方式</param>
        /// <param name="strAcceptLanguage">客户端所能识别的语言</param>
        /// <returns></returns>
        public static string GetWebPageByHttpWeb(string sUrl, Encoding encoding, string strAcceptLanguage)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sUrl);
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)";
            request.Headers.Add("Accept-Language", strAcceptLanguage);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            if (response.StatusCode == HttpStatusCode.OK)
            {
                StreamReader reader = new StreamReader(response.GetResponseStream(), encoding);
                string str = reader.ReadToEnd();
                reader.Close();
                response.Close();
                return str;
            }
            throw new WebException("连接失败", WebExceptionStatus.ConnectFailure);
        }

        /// <summary>
        /// 以WebClient方式抓取网页内容
        /// </summary>
        /// <param name="sUrl">网页地址</param>
        /// <returns>string:网页内容</returns>
        public static string GetWebPageByWeb(string sUrl)
        {
            byte[] bytes = new WebClient().DownloadData(sUrl);
            return GetWebPageEncoding(sUrl).GetString(bytes);
        }

        /// <summary>
        /// 获取网页编码
        /// </summary>
        /// <param name="sUrl">网页地址</param>
        /// <returns>Encoding:网页编码</returns>
        public static Encoding GetWebPageEncoding(string sUrl)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sUrl);
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)";
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    WebHeaderCollection headers = response.Headers;
                    for (int i = 0; i < headers.Count; i++)
                    {
                        if (headers.Keys[i].Equals("Content-Type"))
                        {
                            Match match = new Regex(@"charset=([\w-]*)\.*", RegexOptions.IgnoreCase).Match(headers[i]);
                            if (match.Success)
                            {
                                return Encoding.GetEncoding(match.Groups[1].ToString());
                            }
                        }
                    }
                }
                return Encoding.Default;
            }
            catch
            {
                return Encoding.Default;
            }
        }
    }
}
